<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        $disco = new Application_Model_DbTable_Administrator();
        $this->view->disco = $disco->getAllDisco();

        /* Initialize action controller here */
        require_once "models/Db/Event_db.php";
        require_once "models/Utility/Utility.php";

        $utility_handle = new Utility();
        $eventHandle    = new Event_db();

        $pivaList = $eventHandle->getAllPiva();
        foreach ($pivaList as $pivaList2){
                $piva = $pivaList2['piva'];
                $temp = $eventHandle->getUniqueNamebyPiva($piva);
                $allDisco[$piva] = array ($temp['unique_name'], $temp['name']);
        }
        
		$allDisco = $utility_handle->subval_sort($allDisco,0);
        $this->view->allDisco = $allDisco;
        
        foreach ($pivaList as $pivaList2){
            $piva = $pivaList2['piva'];
            $temp = $eventHandle->getPaeseDsicoByPiva($piva);
            $paese[$piva] = $temp['paese'];
        }
        $this->view->paese = array_unique($paese);


        $category = $eventHandle->getAllCategory();
        $this->view->category = $category;
      
        $zonaList = array('Brescia','Verona','Mantova','Cremona');
        $this->view->zonaList = $zonaList;

    }

    public function indexAction()
    {
        // action body
        $this->_helper->viewRenderer->setNoRender();
        $this->_forward('eventi');
        
    }

    public function eventoAction()
    {
        // action body
        $params = $this->_getParam('id','all');


        if ($params!='all'){
            // string parsing
            $result=explode("_",$params);
            //table=0 => disco, table=1 => risto

            $table=$result[0];
            $id=$result[1];

            $statement1="SELECT * FROM edisco WHERE id='".$id ."'";
            $statement2="SELECT name , full_name FROM immagine_evento WHERE id_event='".$id."'";

           // query part
           $disco = new Application_Model_DbTable_Administrator();
           $discoList=$disco->getStatement($statement1);
           $result=$disco->getStatement($statement2);

           $statement3="SELECT unique_name FROM disco WHERE piva='".$discoList['piva'] ."'";
           $unique_name=$disco->getStatement($statement3);

           $this->view->unique_name = $unique_name['unique_name'];
           $this->view->pren=$discoList['pren'];
           $this->view->id=$discoList['id'];
           $this->view->data=$discoList;
           $this->view->datat=$table; // if is discoEvent or ristoEvent
           $this->view->image=$result['full_name'];
        }

    }

    public function eventiAction()
    {
        // action body
        //cambio il layout della pagina
        $this->_helper->_layout->setLayout('layout_event');

        require_once "models/Db/Event_db.php";
        $eventHandle = new Event_db();

        $unique_name = $this->_getParam('id','noid');
        $day = $this->_getParam('days','all');

        if ($unique_name=='noid'){

            // show disco event
            $show['disco']=1;

            // action body
            try{
                require_once "models/Db/Db_Db.php";

                $db=Db_Db::conn();
                $statement="SELECT * FROM edisco WHERE status=1";
                $discoList=$db->fetchAll($statement);


                // i close the connection
                $db->closeConnection();
            } catch(Zend_Db_Exception $e) {
                echo $e->getMessage();
            }

            /*
             * in $discolist ci sono tutti gli
             * eventi attivi di tutte le disco
             */
            $disco = $discoList;

            /*
             * lista delle disco con eventi attivi
             */
            $pivaList = $eventHandle->discoActiveEvents();

            foreach ($pivaList as $pivaList2){
                    $piva = $pivaList2['piva'];
                    $temp = $eventHandle->getNameDsicoByPiva($piva);
                    $discoName[] = $temp['name'];
                    //$i++;
                    }
            $discoName = array_unique($discoName);
            $discoName=$discoName;

            if ( $day=='all' || !isset($day)){  // have to show all days
                // DO NOTHING
            } else { // i show only event of this day
                //print_r($disco);
                foreach( $disco as $key => $event){
                    $tdata=$event['data_inizio'];
                    if ($tdata==$day){
                         // DO NOTHING
                    }else{ // i remove it

                    if ($tdata==$day){
                            ;//nothing
                        }else { // i remove it
                            unset($disco[$key]);
                        }
                    }
               }//end foreach
            }

            // send data to the view
            $this->view->discoName= $discoName;

        } else {

            $piva    = $eventHandle->getPivaByUniqueName($unique_name);
            //print_r("<pre>". $piva . "</pre>");
            $id_piva = $piva['piva'];
            //print_r("<pre>". $id_piva . "</pre>");
            try{
                require_once "models/Db/Db_Db.php";

                $db=Db_Db::conn();
                $statement="SELECT * FROM edisco WHERE status=1 AND piva=$id_piva";
                $discoList=$db->fetchAll($statement);

                // i close the connection
                $db->closeConnection();
            } catch(Zend_Db_Exception $e) {
                echo $e->getMessage();
            }
            $disco = $discoList;

            
            $discoName = $eventHandle->getNameDsicoByPiva($id_piva);
            $this->view->discoName= $discoName;

        }

        // i reformat the date to a more usable format
        foreach( $disco as $key => $event){

                $tdata=$event['data_inizio'];

                //i set a better format for the date to show
                $yyyy=strtok($tdata, "-");
                $mm=strtok("-");
                $dd=strtok("-");
                //echo "year:  ". $yyyy ."mm: " . $mm . "dd: ". $dd;;
                $disco[$key]['data_inizio']=$dd . "/" . $mm . "/" . $yyyy;
         }

         $this->view->day =$day;
         $this->view->disco=$disco;
    }
    public function array_Unchunk($array) {
		if ($array)
	        return call_User_Func_Array('array_Merge',$array);
    }

    public function showTownAction()
    {
        // action body
        
//        $this->_helper->_layout->setLayout('layout_event');
//        $paese = $this->_getParam('name','err2');
//
//        $this->view->paese2 = $paese;
//        require_once "models/Db/Event_db.php";
//        $eventHandle = new Event_db();
//        $pivaList = $eventHandle->getPivaDsicoByPaese($paese);
//        //$pivaList = $pivaList[0];
//        //$pivaList = $this->array_Unchunk($pivaList);
//
//        $this->view->pivaList = $pivaList;
//
//
//        foreach ($pivaList as $pivaList2){
//            try{
//                require_once "models/Db/Db_Db.php";
//
//                $db=Db_Db::conn();
//                $statement="SELECT * FROM edisco WHERE status=1 AND piva='" . $pivaList2['piva'] . "'";
//
//                $temp=$db->fetchAll($statement);
//                $eventList[] = $temp;
//
//
//                // i close the connection
//                $db->closeConnection();
//
//            } catch(Zend_Db_Exception $e) {
//                echo $e->getMessage();
//            }
//        }
//        $eventList=$this->array_Unchunk($eventList);
//        $this->view->eventList = $eventList;
    }

    public function categoryAction()
    {
        // action body
                $this->_helper->_layout->setLayout('layout_event');
                $categoria = $this->_getParam('id','err');
        
                if ($categoria == "discoteche") {
                    $categoria_name = "Discoteche";
                    $categoria = 1;
                } elseif ($categoria == "bar-pub") {
                    $categoria_name = "Bar - Pub";
                    $categoria = 2;
                } elseif ($categoria == "elegancecafe") {
                    $categoria_name = "Elegance caf&egrave;";
                    $categoria = 3;
                }
        
                $eventHandle = new Event_db();
                $pivaList = $eventHandle->getPivaDiscoByCategory($categoria);
        
                $this->view->categoria_name = $categoria_name;
                $this->view->categoria = $categoria;
        
                require_once "models/Db/Event_db.php";
                $eventHandle = new Event_db();
                if ($pivaList){
                    foreach ($pivaList as $key => $pivaList2){
                        $discoList = $eventHandle->getNameDsicoByPiva($pivaList2['piva']);
                        try{
                            require_once "models/Db/Db_Db.php";

                            $db=Db_Db::conn();
                            $statement="SELECT * FROM edisco WHERE status=1 AND piva='" . $pivaList2['piva'] . "'";
                            $temp=$db->fetchAll($statement);

                            $eventList[] = $temp;

                            // i close the connection
                            $db->closeConnection();

                        } catch(Zend_Db_Exception $e) {
                            echo $e->getMessage();
                        }

                    }
                    $eventList=$this->array_Unchunk($eventList);

                }else {
                    $pivaList = null;
                    $discoList = null;
                    $eventList = null;
                }
                $this->view->pivaList  = $pivaList;
                $this->view->discoList = $discoList;
                $this->view->eventList = $eventList;
    }

    public function provinciaAction()
    {
        // action body
                $this->_helper->_layout->setLayout('layout_event');
                $zona = $this->_getParam('id','err');
        
                $eventHandle = new Event_db();
                $pivaList = $eventHandle->getPivaDiscoByZona($zona);
        
                $this->view->pivaList = $pivaList;
                $this->view->zona = $zona;
        
                require_once "models/Db/Event_db.php";
                $eventHandle = new Event_db();
                foreach ($pivaList as $key => $pivaList2){
                    $discoList = $eventHandle->getNameDsicoByPiva($pivaList2['piva']);
                    try{
                        require_once "models/Db/Db_Db.php";
        
                        $db=Db_Db::conn();
                        $statement="SELECT * FROM edisco WHERE status=1 AND piva='" . $pivaList2['piva'] . "'";
                        $temp=$db->fetchAll($statement);
        
                        $eventList[] = $temp;
        
                        // i close the connection
                        $db->closeConnection();
        
                    } catch(Zend_Db_Exception $e) {
                        echo $e->getMessage();
                    }
                }
                if (!$pivaList){
                    $this->view->discoList = 0;
                    $this->view->eventList = 0;
                
                } else {
                    $eventList=$this->array_Unchunk($eventList);
        
                    $this->view->discoList = $discoList;
                    $this->view->eventList = $eventList;
                }
    }

    /*
     *
     */
    public function dataAction()
    {
        $this->_helper->_layout->setLayout('layout_event');
        // action body
        require_once "models/Db/Event_db.php";
        $eventHandle = new Event_db();
 
        $feste = $this->_getParam('id','err');
        /*
         * lista delle disco con eventi attivi
         */
        $pivaList = $eventHandle->discoActiveEventsByData($feste);
		
		$discoName = array();
        foreach ($pivaList as $pivaList2){
                $piva = $pivaList2['piva'];
                $temp = $eventHandle->getNameDsicoByPiva($piva);
                $discoName[] = $temp['name'];
                //$i++;
                }
        $discoName = array_unique($discoName);
        $discoName=$discoName;

        $eventiData = $eventHandle->getEventByDate($feste);
       // send data to the view
        $this->view->discoName  = $discoName;
        $this->view->feste      = $feste;
        $this->view->eventiData = $eventiData;
    }


}


